package com.xbts.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.CnCloudMarket;
import com.xbts.system.service.ICnCloudMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbts.system.mapper.CnCloudMarketSkuMapper;
import com.xbts.system.domain.CnCloudMarketSku;
import com.xbts.system.service.ICnCloudMarketSkuService;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;

/**
 * 云市场规格Service业务层处理
 * 
 * @author xbts
 * @date 2023-08-22
 */
@Service
public class CnCloudMarketSkuServiceImpl  extends ServiceImpl<CnCloudMarketSkuMapper, CnCloudMarketSku> implements ICnCloudMarketSkuService {

    @Autowired
    private ICnCloudMarketService cnCloudMarketService;

    /**
     * 查询云市场规格列表
     *
     * @param cnCloudMarketSku 云市场规格
     * @return 云市场规格
     */
    @Override
    public List<CnCloudMarketSku> selectCnCloudMarketSkuList(CnCloudMarketSku cnCloudMarketSku) {
        QueryWrapper<CnCloudMarketSku> queryWrapper = cnCloudMarketSku.queryWrapper();
        return list(queryWrapper);
    }

    /**
     * 查询云市场规格
     *
     * @param id 云市场规格主键
     * @return 云市场规格
     */
    @Override
    public CnCloudMarketSku selectCnCloudMarketSkuById(String id) {
        return getById(id);
    }

    /**
     * 新增云市场规格
     *
     * @param cnCloudMarketSku 云市场规格
     * @return 结果
     */
    @Override
    @Transactional
    public boolean insertCnCloudMarketSku(CnCloudMarketSku cnCloudMarketSku) {
        cnCloudMarketSku.setCreateBy(SecurityUtils.getUserId().toString());
        cnCloudMarketSku.setCreateTime(new Date());

        Long marketId = cnCloudMarketSku.getMarketId();
        String firstSkuName = cnCloudMarketSku.getFirstSkuName();
        String secondSkuName = cnCloudMarketSku.getSecondSkuName();
        String cloudMarketBuyingMethod = cnCloudMarketSku.getCloudMarketBuyingMethod();

        // 判断规格是否存在
        CnCloudMarketSku one = this.getOne(new LambdaQueryWrapper<CnCloudMarketSku>()
                .eq(CnCloudMarketSku::getMarketId, marketId)
                .eq(CnCloudMarketSku::getFirstSkuName, firstSkuName)
                .eq(CnCloudMarketSku::getCloudMarketBuyingMethod, cloudMarketBuyingMethod)
                .eq(StringUtils.isNotEmpty(secondSkuName), CnCloudMarketSku::getSecondSkuName, secondSkuName));
        if(StringUtils.isNotNull(one)){
            throw new ServiceException("该SKU已存在!");
        }

        // 获取一级规格下的规格类型
        List<CnCloudMarketSku> list = this.list(new LambdaQueryWrapper<CnCloudMarketSku>()
                .eq(CnCloudMarketSku::getMarketId, marketId)
                .eq(CnCloudMarketSku::getFirstSkuName, firstSkuName)
                .eq(StringUtils.isNotEmpty(secondSkuName), CnCloudMarketSku::getSecondSkuName, secondSkuName));
        if(list.size() > 0){
            CnCloudMarketSku sku = list.get(0);
            if(StringUtils.isNotEmpty(sku.getSecondSkuName()) && StringUtils.isEmpty(secondSkuName)){
                throw new ServiceException("该SKU需填写二级规格!");
            }
            if(StringUtils.isEmpty(sku.getSecondSkuName()) && StringUtils.isNotEmpty(secondSkuName)){
                throw new ServiceException("该SKU不可填写二级规格!");
            }

            if(sku.getCloudMarketBuyingMethod().equals("0")){
                throw new ServiceException("已存在按次购买的同类型SKU!");
            } else {
                if(cloudMarketBuyingMethod.equals("0")){
                    throw new ServiceException("该SKU购买方式不可按次!");
                }
            }
        }
        save(cnCloudMarketSku);
        updateCnCloudMarketLowestPrice(cnCloudMarketSku.getMarketId());
        return true;
    }

    private void updateCnCloudMarketLowestPrice(Long id){
        CnCloudMarket cloudMarket = cnCloudMarketService.getById(id);
        Double LowestPrice;
        List<CnCloudMarketSku> list = list(new LambdaQueryWrapper<CnCloudMarketSku>()
                .eq(CnCloudMarketSku::getMarketId, id)
                .orderByAsc(CnCloudMarketSku::getPrice));
        if(list.size() <= 0){
            LowestPrice = -1D;
        } else {
            LowestPrice = list.get(0).getPrice().doubleValue();
        }
        cloudMarket.setLowestPrice(LowestPrice);
        cnCloudMarketService.updateById(cloudMarket);
    }

    /**
     * 修改云市场规格
     *
     * @param cnCloudMarketSku 云市场规格
     * @return 结果
     */
    @Override
    @Transactional
    public boolean updateCnCloudMarketSku(CnCloudMarketSku cnCloudMarketSku) {
        cnCloudMarketSku.setUpdateBy(SecurityUtils.getUserId().toString());
        cnCloudMarketSku.setUpdateTime(DateUtils.getNowDate());
        updateById(cnCloudMarketSku);
        updateCnCloudMarketLowestPrice(cnCloudMarketSku.getMarketId());
        return true;
    }

    /**
     * 批量删除云市场规格
     *
     * @param ids 需要删除的云市场规格主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteCnCloudMarketSkuByIds(List<String> ids) {
        List<CnCloudMarketSku> list = list(new LambdaQueryWrapper<CnCloudMarketSku>()
                .in(CnCloudMarketSku::getMarketSkuId, ids));
        List<Long> collect = list.stream().map(CnCloudMarketSku::getMarketId).distinct().collect(Collectors.toList());
        removeBatchByIds(ids);
        for (Long aLong : collect) {
            updateCnCloudMarketLowestPrice(aLong);
        }

        return true;
    }

    /**
     * 删除云市场规格信息
     *
     * @param id 云市场规格主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteCnCloudMarketSkuById(String id) {
      return removeById(id);
    }

    @Override
    public List<CnCloudMarketSku> getLowerLevelSku(String marketIds) {
        return baseMapper.getLowerLevelSku(marketIds);
    }

}
